<template>
    <div class="xygshiyong">
        <div class="header" style="display: flex;justify-content: space-between;padding-top: 20px;">
            <!-- <router-link to="/main/renshi/renshizhidu" style="position: absolute;top:10px; left: 10px;">返回</router-link> -->
            <el-button type="primary" @click="$router.push('/main/renshi/renshizhidu')"
                icon="el-icon-back">返回</el-button>
            <span>
                <el-button type="success" icon="el-icon-check" @click="saveForm" v-if="!isView">保存</el-button>
                <el-button type="" icon="el-icon-menu" @click="goToList">列表</el-button>
                <el-button type="info" icon="el-icon-printer" @click="printForm">打印</el-button>

            </span>
        </div>
        <h2>新员工实习试用表</h2>
        <form @submit.prevent="saveForm">
            <table>
                <tr>
                    <td>姓名</td>
                    <td><input type="text" v-model="form.name" required :disabled="isView"></td>
                    <td>工号</td>
                    <td><input type="text" v-model="form.employeeId" required :disabled="isView"></td>
                    <td>职位</td>
                    <td><input type="text" v-model="form.position" required :disabled="isView"></td>
                    <td>部门/组别</td>
                    <td><input type="text" v-model="form.department" required :disabled="isView"></td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td><input type="text" v-model="form.gender" required :disabled="isView"></td>
                    <td>年龄</td>
                    <td><input type="text" v-model="form.age" required :disabled="isView"></td>
                    <td>学历</td>
                    <td><input type="text" v-model="form.education" required :disabled="isView"></td>
                    <td>政治面貌</td>
                    <td><input type="text" v-model="form.politicalStatus" required :disabled="isView"></td>
                </tr>
                <tr>
                    <th colspan="8" style="text-align: left;">工作经历（进入本厂前）：</th>
                </tr>
                <tr>
                    <td colspan="8"><textarea rows="4" v-model="form.workExperience" :disabled="isView"></textarea></td>
                </tr>
                <!-- <tr>
                    <th colspan="8"></th>
                </tr> -->
                <tr>
                    <td colspan="8">
                        实习试用时间（一个星期）：
                        <input type="date" v-model="form.internshipStartDate" required :disabled="isView">
                        至
                        <input type="date" v-model="form.internshipEndDate" required :disabled="isView">
                    </td>
                </tr>
                <tr>
                    <th colspan="8">实习期间表现：</th>
                </tr>
                <tr v-for="(item, index) in form.items" :key="index">
                    <td style="text-align: left;">{{ item.question }}</td>
                    <td>
                        <input type="radio" v-model="item.answer" value="好" :disabled="isView">好
                    </td>
                    <td colspan="2">
                        <input type="radio" v-model="item.answer" value="良好" :disabled="isView">良好
                    </td>
                    <td colspan="2">
                        <input type="radio" v-model="item.answer" value="较好" :disabled="isView">较好
                    </td>
                    <td colspan="2">
                        <input type="radio" v-model="item.answer" value="不好" :disabled="isView">不好
                    </td>
                </tr>
                <tr>
                    <td colspan="1">意见：</td>
                    <td colspan="7"><textarea rows="4" v-model="form.opinion" :disabled="isView"></textarea></td>
                </tr>
                <tr>
                    <td colspan="4">组长签名：<input style="width: 70%;" type="text" v-model="form.groupLeaderApprover"
                            :disabled="isView"></td>
                    <td colspan="4">主管签名：<input style="width: 70%;" type="text" v-model="form.presidentApprover"
                            :disabled="isView"></td>
                </tr>
                <tr>
                    <td colspan="8">
                        注意： 1、各组长根据新员工的资料、实习表现可以决定是否可以留用；<br>&emsp;&emsp;&ensp;2、此表由直属组长填写，并在实习完成后交企业管理部存档。
                    </td>
                </tr>
            </table>
        </form>
    </div>
</template>

<script>
import { getInternInfoDetail, saveInternInfo } from '@/api/internInfo'
import { Message } from 'element-ui'

export default {
    data() {
        return {
            isView: false,
            isEdit: false,
            form: {
                id: null,
                name: '',
                employeeId: '',
                position: '',
                department: '',
                gender: '',
                age: '',
                education: '',
                politicalStatus: '',
                workExperience: '',
                internshipStartDate: '',
                internshipEndDate: '',
                items: [
                    { question: '1) 工作态度是否端正', answer: '' },
                    { question: '2) 对工作接受能力', answer: '' },
                    { question: '3) 对工艺流程了解程度', answer: '' },
                    { question: '4) 与同事相处', answer: '' },
                    { question: '5) 工作积极性', answer: '' },
                    { question: '6) 对上司指定的工作完成情况', answer: '' },
                    { question: '7) 沟通能力', answer: '' },
                    { question: '8) 组织纪律', answer: '' },
                    { question: '9) 团队意识', answer: '' },
                    { question: '10) 品质表现', answer: '' }
                ],
                opinion: '',
                groupLeaderApprover: '',
                presidentApprover: ''
            }
        };
    },
    created() {
        // 获取路由参数
        const { id, type } = this.$route.query;
        if (id) {
            this.form.id = id;
            if (type === 'view') {
                this.isView = true;
            } else if (type === 'edit') {
                this.isEdit = true;
            }
            this.getDetail(id);
        }
    },
    methods: {
        async getDetail(id) {
            try {
                const response = await getInternInfoDetail(id);
                if (response.data && response.data.code === 200) {
                    const data = response.data.data;
                    // 将后端数据映射到表单
                    Object.assign(this.form, data);
                } else {
                    Message.error(response.data?.message || '获取详情失败');
                }
            } catch (error) {
                console.error('获取详情失败：', error);
                Message.error('获取详情失败：' + error.message);
            }
        },
        goToList() {
            this.$router.push('/main/renshi/renshizhidu/xygshiyong/xygshiyongList');
        },
        validateForm() {
            if (!this.form.name) {
                Message.error('请输入姓名');
                return false;
            }
            if (!this.form.employeeId) {
                Message.error('请输入工号');
                return false;
            }
            if (!this.form.department) {
                Message.error('请输入部门');
                return false;
            }
            if (!this.form.position) {
                Message.error('请输入职位');
                return false;
            }
            return true;
        },
        async saveForm() {
            if (!this.validateForm()) {
                return;
            }

            try {
                console.log('准备保存数据:', this.form);
                const response = await saveInternInfo(this.form);
                console.log('保存响应:', response);

                if (response.data && response.data.code === 200) {
                    Message.success('保存成功');
                    this.$router.push('/main/renshi/renshizhidu/xygshiyong/xygshiyongList');
                } else {
                    Message.error(response.data?.message || '保存失败');
                }
            } catch (error) {
                console.error('保存失败：', error);
                Message.error('保存失败：' + error.message);
            }
        },
        printForm() {
            window.print()
        }
    }
};
</script>

<style scoped>
.xygshiyong {

    font-family: '宋体', sans-serif;
    padding: 20px;
     background-color: #f9f9f9; 
    border-radius: 8px;
    /* box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); */
    width: 210mm;
    margin: 0 auto;
    min-height: 100vh;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}

td,
th {
    padding: 8px;
    border: 1px solid #ddd;
    text-align: center;
    font-size: 1em;
    white-space: nowrap;
}

textarea {
    width: 100%;
    min-height: 60px;
    resize: vertical;
    /* //禁止调整高度 */
}

/* button {
    margin: 10px;
    padding: 8px 15px;
    background-color: #409EFF;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

button:hover {
    background-color: #66b1ff;
}

button:disabled {
    background-color: #a0cfff;
    cursor: not-allowed;
} */

input[type="text"] {
    width: 90%;
    padding: 4px;
    border: 1px solid #ddd;
}

input[type="date"] {
    width: 20%;
    padding: 4px;
    border: 1px solid #ddd;
}

input[type="radio"] {
    margin-right: 5px;
}

.form-header {
    margin-bottom: 20px;
}

h2 {
    text-align: center;
    /* margin: 10px 0; */
}

@media print {

    body,
    html {
        margin: 0;
        padding: 0;
    }

    .header {
        display: none !important;
    }

    .leave-adjustment-form {
        margin: 0;
        padding: 0;
        /* page-break-inside: avoid; */
    }

    table {
        /* page-break-inside: avoid; */
        border-collapse: collapse;
        box-shadow: none;
    }

    table th,
    table td {
        border: 1px solid #000;
    }

    input,
    textarea {
        border: none !important;
        background-color: transparent !important;
        text-align: center;
        resize: none;
    }
}
</style>